<!-- 提现账户 -->
<template>
	<view class="withdrawal-account-page">
		<view class="tx-card">
			<view class="tx-top padding30 flex-align-center">
				<image src="/static/technician/zfb.png"></image>
				<text>支付宝申请提现账户</text>
			</view>
			<view class="account-cls padding30 flex-align-center">
				<view>账户姓名</view>
				<input placeholder="请输入账户姓名" v-model="accountInfo.accountName" />
			</view>
			<view class="account-cls padding30 flex-align-center">
				<view>账户号码</view>
				<input maxlength="11" type="number" placeholder="请输入账户号码" v-model="accountInfo.accountNumber" />
			</view>
			<view class="width100 flex"><view class="save-btn flex" @click="saveSet">保存设置</view></view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			isNew: true, //是否为新增账户，默认位是true，为新增账户
			accountInfo: {} //支付宝账户信息
		};
	},
	onShow() {
		this.queryAccount();
	},
	methods: {
		// 提现账户查询（可共用）
		async queryAccount() {
			const result = await this.$http.get({
				url: this.$api.getWithdrawalAccount,
				data: {
					userId: uni.getStorageSync('userId') //登录用户的id
				}
			});
			if (result.code == 200) {
				console.log('提现账户查询result.', result);
				this.isNew = result.data ? false : true;
				if (result.data) {
					this.accountInfo = result.data;
				}
			} else {				
				this.$sun.toast(result.message)
			}
		},
		saveSet() {
			if (!this.accountInfo.accountName) {
				this.$sun.toast('请输入账户名称');
				return false;
			} else if (!this.accountInfo.accountNumber) {
				this.$sun.toast('请输入账户号码');
				return false;
			} else if (!/^1[3456789]\d{9}$/.test(this.accountInfo.accountNumber)) {
				this.$sun.toast('请输入正确的账户号码');
				return false;
			} else {
				// isNew:true_新账户  false_修改账户
				if (this.isNew) {
					this.newAccount();
				} else {
					this.modityAccount();
				}
			}
		},
		// 提现账户新增（可共用）
		async newAccount() {
			const result = await this.$http.post({
				url: this.$api.withdrawalAccount,
				data: {
					accountName: this.accountInfo.accountName, //账户名称
					accountNumber: this.accountInfo.accountNumber, //账户号码
					siteId: uni.getStorageSync('siteId'), // 站点id
					userId: uni.getStorageSync('userId') //登录用户的id
				}
			});
			if (result.code == 200) {
				this.$sun.toast("添加账户成功")
				setTimeout(()=>{
					this.$sun.navBack()
				},1500)
				console.log('提现账户新增result.', result);
			} else {
				this.$sun.toast(result.message)
			}
		},
		// 提现账户修改（可共用）
		async modityAccount() {
			const result = await this.$http.put({
				url: this.$api.withdrawalAccount,
				data: {
					id: this.accountInfo.id, //提现账户主键
					accountName: this.accountInfo.accountName, //账户名称
					accountNumber: this.accountInfo.accountNumber //账户号码
				}
			});
			if (result.code == 200) {
				this.$sun.toast("修改账户成功")
				setTimeout(()=>{
					this.$sun.navBack()
				},1500)
				console.log('提现账户修改result.', result);
			} else {
				this.$sun.toast(result.message)
			}
		}
	}
};
</script>

<style lang="less">
page {
	background-color: #f7f7f7;
}
.withdrawal-account-page {
	.tx-card {
		margin: 30rpx;
		border-radius: 40rpx;
		background: #fff;
		.tx-top {
			border-bottom: 2rpx solid #f1f1f1;
			image {
				width: 60rpx;
				height: 60rpx;
				margin-right: 20rpx;
			}
			view {
				font-size: 32rpx;
			}
		}
		.account-cls {
			border-bottom: 2rpx solid #f1f1f1;
			view {
				font-size: 32rpx;
				margin-right: 54rpx;
			}
			input {
				flex: 1;
			}
		}
		.save-btn {
			margin: 58rpx 0 52rpx;
			color: #fff;
			font-size: 32rpx;
			width: 634rpx;
			height: 88rpx;
			border-radius: 20rpx;
			background: #1890ff;
		}
	}
}
</style>
